﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Himp.Platform.Api
{
    [Route("openApi/v1")]
    public class OpenApiController: ApiController
    {
        private IOpenApiService _openApiService;
        private IWorkspaceConfService _workspaceConfService;
        private ILogger<OpenApiController> _logger;

        public OpenApiController(IOpenApiService openApiService
            , IWorkspaceConfService workspaceConfService
            , ILogger<OpenApiController> logger)
        {
            _openApiService = openApiService;
            _workspaceConfService = workspaceConfService;
            _logger = logger;
        }

        [HttpPost]
        public async Task<OpenApiOutput> Post([FromBody] OpenApiInput input)
        {
            OpenApiOutput openApiOutput = new OpenApiOutput();
            openApiOutput.InfRefMsgId = input.MsgId;

            #region 机构信息验证

            WorkspaceConfDto? workspaceConf = null;

            try
            {
                WorkspaceConfInput workspaceConfInput = new WorkspaceConfInput();
                workspaceConfInput.WorkspaceCodg = input.OrganCode;
                var workspaceConfs = await _workspaceConfService.GetListAsync(workspaceConfInput);
                workspaceConf = workspaceConfs.Items.FirstOrDefault();

                if (workspaceConf == null)
                {
                    return openApiOutput.ReturnError("无效的机构信息");
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                _logger.LogError(e.StackTrace);
                return openApiOutput.ReturnError("获取机构信息异常");
            }

            string secretKey = workspaceConf.SecretKey;
            string aesKey = workspaceConf.AesKey;

            #endregion

            #region 验证参数

            if (!OpenApiControllerHelper.ValidateHeader(input.InfNo, input.MsgId, input.Input, secretKey, input.Cainfo))
            {
                return openApiOutput.ReturnError("Cainfo参数验证失败");
            }

            #endregion

            #region Input解析

            try
            {
                if (input.SignType == "AES")
                {
                    var tmp = OpenApiControllerHelper.AesEncrypt(input.Input, aesKey);
                    input.Input = OpenApiControllerHelper.AesDecrypt(input.Input, aesKey);
                }

                _logger.LogInformation(input.Input);
            }
            catch
            {
                var err = "Input参数解析失败";
                _logger.LogError(err);
                return openApiOutput.ReturnError(err);
            }

            try
            {
                openApiOutput = await _openApiService.Post(input.MsgId, input.InfNo, input.Input);
                openApiOutput.RespondTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                _logger.LogError(e.StackTrace);
                return openApiOutput.ReturnError("系统内部未处理的异常");
            }

            return openApiOutput;

            #endregion
        }
    }
}
